<template>
  <!--  <top-header/>-->
  <div class="container">

    <el-row>
      <el-col :span="24" class="user_top">

        <div v-if="lessiondata.isMyCourse" style="margin-left:20px;display:flex;align-items: center;float:left;vertical-align: middle;height:100%">
          <a href="/public#/courselist"><el-icon><Back /></el-icon></a>&nbsp;
        </div>
        <div v-else style="margin-left:20px;display:flex;align-items: center;float:left;vertical-align: middle;height:100%">
          <a href="/public#/course"><el-icon><Back /></el-icon></a>
        </div>
        <div class="user_img">
          <h3>{{name}}</h3>
        </div>
      </el-col>
      <el-col :span="24">
      </el-col>
    </el-row>

    <div class="common-layout">
      <el-container>
        <el-main>
          <div class="content-panel header-panel">
            <div class="content">
              Content
            </div>
            <div class="button" v-if="lessiondata.isMyCourse">
              <el-button type="primary" :icon="Plus" circle @click="showdialog()" />
            </div>
          </div>


          <div class="content-panel lesson-card" v-for="item in lessiondata.list">
            <div class="content">
              {{item.name}}&nbsp;{{item.starttime}}
            </div>
            <div class="button">
              <a :href="'/#/liveStu?courseid='+courseid+'&lessionid='+item.id" target="_blank">
                <el-button type="primary" :icon="DataAnalysis" circle class="action-btn" v-if="!lessiondata.isMyCourse"/>
              </a>
              <el-button type="warning" :icon="DataAnalysis" circle  @click="toLession(item)" v-if="lessiondata.isMyCourse"/>
<!--              <el-button type="warning" :icon="DataAnalysis" circle  @click="toLession22(item)">22222</el-button>-->
              <el-button type="success" :icon="Link" circle @click="doClassRoom(item.id)"/>
              <el-button type="primary" :icon="Edit" circle  @click="doEdit(item)" v-if="lessiondata.isMyCourse"/>
              <el-button type="danger" :icon="Delete" circle @click="doDelete(item.id)" v-if="lessiondata.isMyCourse"/>
            </div>
          </div>

        </el-main>

        <el-aside>

          <div class="content-panel header-panel">
            <div class="content">
              Notification
            </div>
            <div class="button" v-if="lessiondata.isMyCourse">
              <el-button type="primary" :icon="Plus" circle @click="noticeshowdialog()"/>
            </div>
          </div>

          <div class="content-panel2" v-for="item in noticedata.list">
            <div class="notice-card">
              <div class="content2">
                <div class="notice-title">{{item.title}}</div>
                <div class="notice-content">{{item.content}}</div>
              </div>
              <div class="button2">
                <el-button type="danger" :icon="Delete" circle @click="doDeleteNotice(item.id)" v-if="lessiondata.isMyCourse"/>
              </div>
            </div>
            <div class="notice-time">{{item.createtime}}</div>
          </div>

        </el-aside>
      </el-container>
    </div>

  </div>
  <!-- 底部信息 -->
  <!--  <top-bottom/>-->
  <!-- lession dialog -->
  <el-dialog v-model="dialogFormVisible.visible" title="save&edit" width="500" class="custom-dialog">
    <el-form :model="form">
      <el-form-item label="name" :label-width="formLabelWidth">
        <el-input v-model="onelession.id" type="hidden"/>
        <el-input v-model="onelession.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="starttime" :label-width="formLabelWidth">
        <el-date-picker
            v-model="onelession.starttime"
            type="datetime"
            placeholder="Select date and time"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible.visible = false">Cancel</el-button>
        <el-button type="primary" @click="doSubmit()">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
  <!-- notice dialog -->
  <el-dialog v-model="noticedialogFormVisible.visible" title="save&edit" width="500" class="custom-dialog">
    <el-form :model="form">
      <el-form-item label="title" :label-width="formLabelWidth">
        <el-input v-model="onenotice.id" type="hidden"/>
        <el-input v-model="onenotice.title" autocomplete="off" />
      </el-form-item>
      <el-form-item label="content" :label-width="formLabelWidth">
        <el-input v-model="onenotice.content" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="noticedialogFormVisible.visible = false">Cancel</el-button>
        <el-button type="primary" @click="doSubmitNotice()">
          Confirm
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>

//import TopHeader from '../../components/TopHeader.vue' // 引用Header组件
//import TopBottom from '../../components/TopBottom.vue' // 引用Header组件
import {
  DataAnalysis,
  Link,
  Delete,
  Edit,
  Message,
  Plus,
  Star,
} from '@element-plus/icons-vue'
import { reactive,ref,onMounted} from 'vue'

import { getMyLessionList,deleteLession,saveLession } from '../../api/lession.ts'
import { getCourseNoticeList,deleteNotice,saveNotice } from '../../api/notice.ts'
import { isMyCourse,updateCourseLive } from '../../api/course.ts'
import { useRoute,useRouter } from 'vue-router'

const route = useRoute()
const currentRoute = useRouter()
const courseid = route.query.courseid
const name = route.query.name
const dialogFormVisible = reactive({
  visible:false
})
const noticedialogFormVisible = reactive({
  visible:false
})
const formLabelWidth = '140px'

const lessiondata = reactive({
  list:[],
  isMyCourse:false
})

const noticedata = reactive({
  list:[]
})

//单门课程lession，增加和编辑使用
const onelession = reactive({
  id:null,
  name:'',
  starttime:'',
  courseid:courseid
})

const onenotice = reactive({
  id:null,
  title:'',
  content:'',
  courseid:courseid
})
function toLession(item)
{
  //currentRoute.push('/live.html?courseid='+courseid+'&lessionid='+item.id);
  updateCourseLive({"id":courseid,"type":1}).then((res) => {
    if (res.code == 200)
    {
      const link = document.createElement('a');
      link.href = '/live.html?courseid='+courseid+'&lessionid='+item.id;
      link.target = '_blank';
      link.click();
      //currentRoute.push('/room?courseid='+courseid+"&roomId="+item.id+"&role=broadcaster","_blank")

      // const route = currentRoute.resolve({
      //   path: '/room',
      //   query: {
      //     courseid: courseid,
      //     roomId: item.id,
      //     role: 'broadcaster'
      //   }
      // });
      // window.open(route.href, '_blank');

    }
  })

}

function toLession22(item)
{

  const link = document.createElement('a');
  link.href = '/live.html?courseid='+courseid+'&lessionid='+item.id;
  link.target = '_blank';
  link.click();

  // const route = currentRoute.resolve({
  //   path: '/room',
  //   query: {
  //     courseid: courseid,
  //     roomId: item.id
  //   }
  // });
  // window.open(route.href, '_blank');

}
function doClassRoom(id)
{
  currentRoute.push('/classroom?courseid='+courseid+"&lessionid="+id+"&name="+name)
}
onMounted(() => {
  loaddata();
})

function loaddata()
{
  getMyLessionList({"courseid":courseid}).then((res) => {
    if (res.code == 200)
    {
      lessiondata.list = res.data;
    }
  })

  isMyCourse({"courseid":courseid}).then((res) => {
    if (res.code == 200)
    {
      lessiondata.isMyCourse = res.data;
    }
  })
  getCourseNoticeList({"courseid":courseid}).then((res) => {
    if (res.code == 200)
    {
      noticedata.list = res.data;
    }
  })

}

function doDelete(id)
{
  deleteLession({"id":id}).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function doDeleteNotice(id)
{
  deleteNotice({"id":id}).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function showdialog()
{
  dialogFormVisible.visible = true
  onelession.id=null
  onelession.name=''
  onelession.starttime=''
}
function noticeshowdialog()
{
  noticedialogFormVisible.visible = true
  onenotice.id=null
  onenotice.title=''
  onenotice.content=''
}
function doEdit(item)
{
  dialogFormVisible.visible = true
  onelession.id=item.id
  onelession.name=item.name
  onelession.starttime=item.starttime
}
const formatTime = (time) => {
  //将中国标准时间转化为yyyy-mm-dd h:m:s
  const date = new Date(time);
  var year = date.getFullYear();
  /* 在日期格式中，月份是从0开始的，因此要加0
   * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
   * */
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  // 拼接
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
};
function doSubmit()
{
  dialogFormVisible.visible = false
  const rt = formatTime(onelession.starttime)
  onelession.starttime=rt
  saveLession(onelession).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
function doSubmitNotice()
{
  noticedialogFormVisible.visible = false
  saveNotice(onenotice).then((res) => {
    if (res.code == 200)
    {
      //更新下视图数据
      loaddata();
    }
  })
}
</script>
<style lang="less" scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background-color: #f8fafc;
  min-height: 100vh;
  box-sizing: border-box;
}

.user_top {
  background-color: white;
  padding: 15px 25px;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
  display: flex;
  align-items: center;

  h3 {
    margin: 0;
    font-size: 1.25rem;
    color: #2c3e50;
    font-weight: 600;
    flex-grow: 1;
    text-align: center;
  }

  .el-icon {
    font-size: 1.25rem;
    color: #4f46e5;
    transition: all 0.2s ease;

    &:hover {
      color: #4338ca;
      transform: scale(1.1);
    }
  }
}

.common-layout {
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.el-container {
  min-height: calc(100vh - 160px);
}

.el-main {
  padding: 25px !important;
  background-color: #f8fafc;
}

.el-aside {
  background-color: white;
  border-left: 1px solid #e2e8f0;
  padding: 25px !important;
}

.content-panel {
  background-color: white;
  padding: 16px 20px;
  border-radius: 8px;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: all 0.2s ease;

  &.header-panel {
    background-color: #f1f5f9;
    border-bottom: 1px solid #e2e8f0;
    margin-bottom: 20px;
  }

  &.lesson-card {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }

  .content {
    font-size: 1rem;
    color: #2c3e50;
    flex-grow: 1;

    .notice-title {
      font-weight: 600;
      font-size: 1rem;
      margin-bottom: 5px;
      color: #1e293b;
    }

    .notice-content {
      color: #64748b;
      font-size: 0.875rem;
      line-height: 1.5;
    }
  }

  .button {
    display: flex;
    align-items: center;
    gap: 10px;

    .notice-time {
      color: #94a3b8;
      font-size: 0.8125rem;
      margin-right: 10px;
    }

    .el-button {
      transition: all 0.2s ease;

      &:hover {
        transform: scale(1.1);
      }
    }
  }
}

.custom-dialog1 {
  border-radius: 12px !important;

  .el-dialog__header {
    border-bottom: 1px solid #f1f5f9;
    margin-right: 0;
    padding: 20px;

    .el-dialog__title {
      font-weight: 600;
      color: #2c3e50;
    }
  }

  .el-dialog__body {
    padding: 20px;
  }

  .el-form-item__label {
    color: #64748b;
    font-weight: 500;
  }

  .el-input__inner,
  .el-textarea__inner {
    border-radius: 8px;
    border: 1px solid #e2e8f0;
    transition: all 0.2s ease;

    &:focus {
      border-color: #4f46e5;
      box-shadow: 0 0 0 1px rgba(79, 70, 229, 0.2);
    }
  }

  .el-date-editor {
    width: 100%;
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 0 20px 20px;

    .el-button {
      border-radius: 8px;
      padding: 10px 20px;
      transition: all 0.2s ease;

      &.el-button--primary {
        background-color: #4f46e5;
        border-color: #4f46e5;

        &:hover {
          background-color: #4338ca;
        }
      }
    }
  }
}

.content-panel2 {
  display: flex;
  flex-direction: column;
  //align-items: flex-start;
  //gap: 15px;
  //padding: 15px;
  //padding: 16px 20px;
  border-radius: 8px;
}

.notice-card {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  display: flex;
  flex-direction: row;    /* 子元素横向排列（默认值，可省略） */
  gap: 10px;             /* 子元素间距（可选） */
  padding: 10px;
  &:hover {
    border-color: #c7d2fe;
  }
  .content2 {
    font-size: 1rem;
    color: #2c3e50;
    flex-grow: 9;

    .notice-title {
      font-weight: 600;
      font-size: 1rem;
      margin-bottom: 5px;
      color: #1e293b;
    }

    .notice-content {
      color: #64748b;
      font-size: 0.875rem;
      line-height: 1.5;
    }
  }

  .button2 {
    display: flex;
    align-items: center;
    flex-grow: 1;
    justify-content: flex-end;
  }
}
.notice-time {
  width: 100%;
  margin-bottom: 4px;
  color: #94a3b8;
  font-size: 0.8125rem;
  text-align: right; // 可选：时间右对齐
}

@media (max-width: 768px) {
  .container {
    padding: 15px;
  }

  .el-container {
    flex-direction: column;
  }

  .el-aside {
    width: 100% !important;
    border-left: none;
    border-top: 1px solid #e2e8f0;
    padding: 20px !important;
  }

  //&.notice-card {
  //  border: 1px solid #e2e8f0;
  //  &:hover {
  //    border-color: #c7d2fe;
  //  }
  //}


}
</style>
